package com.astrob.model;

import android.content.Context;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;

import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;

import java.util.ArrayList;

public class HistoryCityFileHandle {
	private static String HISCITYPATH = Start.RUNDIR + "/historycity.dat";
	private static HistoryCityFileHandle instance = new HistoryCityFileHandle();
	private ArrayList<HistroyCityData> mListHistorycity = new ArrayList<HistroyCityData>();

	public static HistoryCityFileHandle getInstance() {
		return instance;
	}

	public ArrayList<HistroyCityData> getListHistroyCity() {
		return mListHistorycity;
	}

	public HistoryCityFileHandle() {
		load();
	}

	public void load() {
		mListHistorycity.clear();
		ReadObjectsFromFile<HistroyCityData> readFile = new ReadObjectsFromFile<HistroyCityData>();
		readFile.onReadObjectsList(HISCITYPATH, mListHistorycity);
	}

	@SuppressWarnings("unchecked")
	public void save() {
		@SuppressWarnings("rawtypes")
		WriteObjectsToFile writeFile = new WriteObjectsToFile();
		writeFile.onWriteObjects(HISCITYPATH, mListHistorycity);
	}
	
	public boolean add(HistroyCityData fav) {
		if (fav == null || haveSameFav(fav)) {
			return false;
		}

		mListHistorycity.add(fav);
		save();
		return true;
	}

	public void delete(int index) {
		if (index >= 0 && index < mListHistorycity.size()) {
			HistroyCityData fav = mListHistorycity.get(index);
			mListHistorycity.remove(index);
			save();
		}
	}
	public void delete(HistroyCityData fav) {
		int index = mListHistorycity.indexOf(fav);
		if (index >= 0 && index < mListHistorycity.size()) {
			HistroyCityData f = mListHistorycity.get(index);
			mListHistorycity.remove(index);
			save();
		}
	}

	public boolean haveSameFav(HistroyCityData fav) {
		if (fav == null || mListHistorycity.size() < 1) {
			return false;
		}
		return mListHistorycity.contains(fav);
	}

	public void removeAll() {
		mListHistorycity.clear();
		save();
	}
}